Using Variables & Expressions

Variables and expressions allow you to customize an experiment or to dynamically control how the stimuli appear during your experiment. We recommend assigning descriptive names to the variables as they may be used in the legends, graph labels, or column headers in the Plotter Panel. Note that variables in upper case ALWAYS refer to system variables and that variable names should only contain alphabetic characters (ie with no numerals, punctuation marks, symbols or space).

IMPORTANT: Variable names may sometimes appear between brackets. The rule about brackets is to use them only when the variable specifies a value (e.g., when specifying a stimulus parameter, or inside expressions), and to not use them when indicating the variable name for control purpose (e.g., dependent variable in method and procedure properties).

Control Variables
Response Variables
Specifying Parameters with Variables
Defining Values with Expressions
Syntax of Mathematical Expressions
System-Defined Constants & Variables

Control Variables

Control variables are typically used to customize a session for each subject or to simplify the parameterization of the experiment. They should be the independent variables of your experimental design. Control variables are declared either under the "Control Settings" tab of the "Experiment" events or the "Conditions" tab of "Method" events when creating experimental conditions.

Declaring independent variables in "Control Settings"

A table is available under "Control Settings" to declare independent variables:

An unlimited number of variables can be entered in the table, along with their values and locking status:

Each variable can either specify:

Note that random variables take new values each time they are used. Hence one should not assume identical values when the same random variable specifies several parameters of the same stimulus (see Defining Values with Expressions below and the use of the $ prefix to access the last variable value without generating a new one).

The locking status indicates whether the variable values can also be specified as subject-specific values at each start of an experiment session. In the example above, only the unlocked variables 'coherence' and 'contrast' can be modified in the "Session Information" panel when running the experiment:

The subject-specific values are shown in the “Subject-Specific Parameters” table where they can be edited, and are used instead of the default values specified in the “Independent Variables” table under the "Control Settings" tab of the Experiment Properties panel.

Declaring independent variables in "Conditions" method events

Control variables can also be specified when creating "Condition" events (see "Conditions" tab of "Method" event). Several variables can be defined along with multiples values to specify each experimental condition as illustrated below:

In the example above, two variables are defined for the condition named "Condition 1" selected in the "Conditions" table: 'size' and 'sf'. The selected variable in the "Variables" table, 'size', can take any of the specified values in the "Values" table ('2', '4' or '8'). Note that these variables values have to be specified with scalars (ie: expressions are not supported yet).

It is important to note as well that independent variables should be declared only once in your experiment design: an error will be emitted at run-time if the same variable is declared both in the "Control Settings" of the "Experiment" events and inside a "Condition" event.

Response Variables

Methods and procedures typically modify the stimuli properties based on the subject's responses and the stimuli intervals. Response variables should be the dependent variables of your experimental design. In each panel where the selection of a response variable is required, a selection box similar to the one shown below appears:

Once a dependent variable is declared, it can then be used to specify some stimulus properties. Failing to correctly specify the response variables and their instances in stimuli parameters (see Specifying Parameters with Variables below) will result in incorrect or undefined control of the stimuli parameters. However, warnings displayed in the "Experiment log" are likely to provide clues about the abnormal behavior of the experiment. Consequently, it is crucial to verify that no warning or error has been logged after the experiment is terminated (remember that pressing the ESC key on the primary keyboard will abort the experiment). For example if the same variable is declared as both a control and response variable, an error will be emitted at run-time.

Specifying Parameters with Variables

Once a variable has been declared either as a control or response variable, it should be used to specify the value taken by a parameter in the different events. Most event parameters can be edited through a text field. To specify a parameter value with a variable, simply replace the numerical value in its text field with the variable name in brackets (ie: [variable]). The variable name can also be chosen by control-clicking on the parameter text field and selecting the variable from the contextual menu (both user-defined variables and system variables are available). The text field is then automatically filled in with the variable in brackets as illustrated below:

  

Note that for randomly-defined variables (see "Control Variables" above), the reference to its value through the [variable] format will trigger the generation of a new random value at run-time. This can be a problem when using the same random variable to specify different properties of the same stimulus or keeping some properties synchronized across different stimuli, because one would expect the same value to be used. To prevent this problem, one can use the format [$variable] (note the $ symbol before the variable name) to indicate to use the last generated value rather than generating a new random value. In other words to ensure a correct behavior, the first reference to a random variable for a given trial should be done through the format [variable] and all subsequent references to this variable should be done through the format [$variable].

For stimuli event, the variable format can also take optionally the form [variable:defaultvalue] where defaultvalue is the value used by default to generate the stimulus preview. If not specified, the parameter value is 0 by default. Note that if an appropriate default value is not provided, the stimulus preview may either lead to an unexpected result or a warning sign due to a computation error (eg: a division by zero). So it is highly recommend to always provide explicit default values for your stimulus parameters to check the validity of your stimuli during your experimental design.

In addition to single numerical values and variables, parameter values can be specified with complex mathematical expressions as described in the next section using the "Math Editor" (also available from the contextual menu).

Tip to remember: control-click on any parameter text field to access the list of available variables or the math editor from the contextual menu.

Defining Values with Expressions

Psykinematix allows most of the stimuli parameters to be defined as mathematical expressions of arbitrary complexity. These expressions may depend on dependent or independent variables of the experimental design, be a function of time to create dynamic stimuli, or even be randomly selected from a specified range. The creation of Custom Stimuli relies entirely on the evaluation of a sequence of such mathematical expressions with the specificity that they make use of built-in 2D and temporal variables (x, y, r, theta, time). These computing capabilities make Psykinematix a powerful and very versatile stimulus generator.

Expressions are used to specify the numerical values taken by most stimuli parameters as well as the decision evaluations in procedures. Valid formats for expressions are:

Time-varying expressions.

Stimuli parameters can be set as time-varying. Several formats are available for specifying time-varying expressions:

Syntax of Mathematical Expressions

A single mathematical expression should follow the <expression> syntax which can have any of the following forms (parentheses can be used to specify the order of operations):

<expression> :=

<numerical value>

<constant>

<variable>

<function>(<expression 1>, ... , <expression n>)

<expression 1> <operator> <expression 2>

where <variable> is either:

[<name>]         any user- or system-defined variable (note that system-defined variables are always in uppercase)

<parameter>    a parameter name defined in the Parameter Table for Custom Stimuli

<reserved>      a reserved 2D array coordinate only available for Custom Stimuli (x, y, r, theta)

<constant> is any of the predefined scalar constants:

  e 2.71828182845904523536     Euler's constant
  pi 3.14159265358979323846     Archimedes' constant
  yes boolean true value
  no boolean false value

  irand Random integer number (can be used as the seed parameter in the unoise function, see below)
  rnd Random real number in [0,1] range

<operator> is any of the following elementary operators:

Arithmetic operators
  +addition
  -subtraction
  *multiplication
  /division
  %modulo
  ^exponent (note: faster than the power function pow below)

Comparison operators
  >"larger than"
  <"smaller than"
  "larger than or equal to" (option+. keystroke)
  "smaller than or equal to" (option+, keystroke)
  "equal to" (option+x keystroke)
  "different from" (option+= keystroke)

Logical operators
  &"and"
  |"or"

Operators grouped by precedence (evaluation priority can be changed using parentheses)
  Unary operators:+, - , !
  Power:^
  Multiple, divide, modulo:*, /, %
  Add, subtract:+, -
  Comparisons:>, ≥, <, ≤
  Comparisons:;≈, ≠
  Logical AND:&
  Logical OR:|

<function> is any of the following built-in functions:

Trigonometric functions:
 sin(x)sine (x given in radians)
 cos(x)cosine (x given in radians)
 tan(x)tangent (x given in radians)
 asin(x)arcsine ⇒ [-π/2, π/2]
 acos(x)arccosine ⇒ [0, π]
 atan(x)arctangent ⇒ [-π/2, π/2]
 atan2(y,x)arctangent of y/x ⇒ [-π,π]
 sinh(x)hyperbolic sine
 cosh(x)hyperbolic cosine
 tanh(x)hyperbolic tangent
 asinh(x)inverse hyperbolic sine
 acosh(x)inverse hyperbolic cosine
 atanh(x)inverse hyperbolic tangent
 dist(x,y)Euclidean distance: sqrt(x^2+y^2)
 deg(x)convert from radian to degree units
 deg(x)convert from degree to radian units
 degtopix(x)convert from degree to pixel units
 pixtodeg(x)convert from pixels to degree units
 exp(x)exponential
 log(x)natural logarithm
 log2(x)base 2 logarithm
 log10(x)base 10 logarithm
 sqrt(x)square root

Non-continuous functions:
 floor(x)floor
 ceil(x)ceiling
 round(x)nearest int

Non-differentiable functions:
 abs(x)absolute value
 sign(x)sign (-1 if x<0,1 if x≥0)
 rect(x)rectification (x if x>0, 0 if x≤0)
 cmin(x,m)minimum clamping (x if x>m, m if x≤m)
 cmax(x,m)maximum clamping (x if x<m, m if x≥m)
 cminmax(x,min,max)clamping in the range [min,max] (x if min<x<max, min if x≤min, max if x≥max)
 clipx,min,max)same as cminmax()
 step(x,m)step function (0 if x>m, 1 if x≤m)
 hsmoothstep(x,m,s)Hermite interpolation between 0 and 1 (in range [m, m+s])

Special functions:
 randg(m,s)generate a random number from Gaussian distribution with mean m and standard deviation s

 
randseries(a,b,c,d)generates a binary time series based on some uniform distribution of its ON and OFF segments where a is the minimum number of consecutive ON bits, b is the random number of additional ON bits (set it to 0 to specify a fixed number of consecutive ON bits, that is a), c is the minimum number of consecutive OFF bits, d is the random number of additional OFF bits (set it to 0 to specify a fixed number of consecutive OFF bits, that is c). To create a time-varying expression based on such a stochastic sequence it is important to include the system-defined [TIME] variable even if it does not appear to be necessary (for example by simply adding '+[TIME]*0' to the expression). Note that this function cannot be directly used inside the expression box of Custom Stimuli, but can specify the values of any stimulus parameter in a text field (eg: Contrast).

The following 2D functions are only available for Custom Stimuli:

Matrix operations:
  shift(x,u,v) Shift array x by u spatial units horizontally, and v spatial units vertically with wrapping and subpixel resolution through bilinear interpolation
  dshiftp(x,d) Shift array x horizontally by array d on a pixel by pixel basis with subpixel resolution (positive shift only!) This function can be used to apply positive and negative disparity in stereo-pair stimuli
  conv(x,y) 2D Convolution of x and y (done in Fourier space)
  mag(x) Fourier magnitude of array x
  phase(x) Fourier phase of array x
  ifft(m,p) Inverse Fourier transform for magnitude array m and phase array p
  norm(x,m,u) Normalization of x so its mean is m and its maximum value is u
  scale(x,a,b) Scale x between a and b

Matrix to Scalar operations:
  min(x) Minimum value of matrix x
  max(x) Maximum value of matrix x
  sum(x) Sum value of matrix x
  length(x) Length of matrix x (n^2 for a n x n matrix)
  mean(x) Mean value of matrix x
  std(x) Standard deviation value of matrix x

Special functions:
  unoise(x,s,g) 2D uniform noise between -1 and 1 with seed s (as integer), granularity g (in pixels) and the same size as x
  gnoise(x,m,u,s,g) 2D Gaussian noise with mean m, std u, seed s (as integer), granularity g (in pixels) and the same size as x
  pnoise(x,y) 2D Perlin noise at coordinate x,y with same size as x and y (fractal or pink noise can be created by rescaling Perlin noise and added into itself)
  bessj0(x) 2D Bessel function of order 0
  bessj1(x) 2D Bessel function of order 1
  bessjn(x,n) 2D Bessel function of order n
  pow(x,y) power (see the more versatile and faster exponent function x^y above)

Note that most 2D operations are applied on a pixel basis when at least one of the operands consists of a 2D array as it is the case for Custom Stimuli. The evaluation of the expressions is also highly optimized, typically several times faster than its Matlab® equivalent.

Expressions specific to trigger information:

The following 2 formats can only be used when specifying some trigger information sent to an external device (see Trigger section):

  <expr 1> == <expr 2> Equality testing returns a boolean value: 1 if the 2 expressions are equal, 0 otherwise
  <expr 1> ? <expr 2> : <expr 3> Conditional statement (if ... then ... else ...): evaluate <expr 2> if <expr 1> is true, evaluate <expr 3> if <expr 1> is false

System-Defined Constants & Variables

Psykinematix uses special built-in constants and variables to keep track of particular values. All system-defined constants and variables are in uppercase to differentiate them from user-defined variables.

System-defined constants

System-defined constants are those values that do not change inside an experimental session and that depend on the experimental setup. Here is a list of these constants:

  [NYQ]Nyquist frequency of the display in cycle per degree (cpd) (see Display Preferences and Experiments sections). This could be used to set a maximum limit to the spatial frequency of visual stimuli.
  [VDIST]Viewing distance in millimeters (see Display Preferences and Experiments sections).
  [FRATE]Frame rate of the display in Hz (see Display Preferences and Experiments sections). This could be used to set a maximum limit to the temporal frequency of visual stimuli (eg, [FRATE]/2 ).
  [WFOV]Width of the display field of view in deg (see Display Preferences and Experiments sections). This could be used to set a maximum limit to the horizontal eccentricity of visual stimuli (eg, [WFOV]/2).
  [HFOV]Height of the display field of view in deg (see Display Preferences and Experiments sections). This could be used to set a maximum limit to the vertical eccentricity of visual stimuli (eg, [HFOV]/2 ).
  [XFIXPOS]x coordinate of the fixation mark relative to the display center expressed in deg (see Experiments section). This could be used to specify the stimulus position relative to the fixation mark.
  [YFIXPOS]y coordinate of the fixation mark relative to the display center expressed in deg (see Experiments section). This could be used to specify the stimulus position relative to the fixation mark.

System-defined dependent variables

System-defined dependent variables are read-only variables whose values change inside an experimental session and that depend on some properties of the experimental events currently run or edited. Here is a list of these read-only variables:

  [DURATION]Duration of the currently edited stimulus. This should be used when defining stimuli parameters as expressions of functions of time (see Expressions section above).
  [FIXCHANGED]Binary value that indicates that a fixation change has occurred in the current trial when 2 stimulus fixations are specified in the Experiment Control Settings. This can be used to introduce a fixation task (change detection) to control for attention in experiments where a succession of stimuli are presented: [FIXCHANGED] should be used as the stimulus correlated expression in a Yes/No Procedure (the writable system-defined variables FIXDURATION and FIXPROB specify the fixation duration and probability, see below).
  [INPUT]Name of the last input provided by the subject coded as a string (see Procedure section).
  [NFC]Index of the current target interval or position in a n-Forced Choice procedure. This could be used to report the procedure target position to an external device for example.
  [NFCINDEX]Index for each stimulus instance in a n-Forced Choice procedure. This could be used to customize the appearance of each instance.
  [RESPONSE]Last response provided by the subject coded as a string that can take one of the following values (the possible values depend on the selected procedure): 'Hit', 'Miss', 'False Alarm', 'Correct Rejection', 'No Response', 'Increment', 'Decrement', 'Stop', 'Yes', 'No', 'Standard', 'Test', 'Unsure' or 'Aborted' (see Procedure section).
  [RT]Reaction time in seconds of the last response provided by the subject (see Procedure section).
  [SELECTION]Value of the currently selected stimulus when the stimulus is chosen from amongst a list (such as for multimedia stimuli through the 'Random selection' presentation mode). For example, it could be used when sending trigger information to an external device (see Trigger section).
  [TIME]Relative time to stimulus onset. This should be used when defining stimuli parameters as expressions of functions of time (see Expressions section above).
  [TRIAL]Index of the current trial. This could be used when presenting stimuli that need to be customized for each trial, for example when performing a retinotopic mapping with expanding rings and rotating wedges with each trial triggered by an external device (eg, fMRI scanner).
  [XMOUSEDEG], [YMOUSEDEG]Current mouse position in visual field coordinates (ie: deg relative to fixation).
  [XMOUSEPIX], [YMOUSEPIX]Current mouse position in screen coordinates (ie: pixels).

System-defined independent variables

System-defined independent variables are writable variables whose values can be specified in the independent variable table of the Experiment Control Settings. These variables allow the customization of some experimental aspects that do not have a counterpart in the graphical user interface. Here is a list of these writable variables:

  [FIXDURATION], [FIXPROB]Fixation duration in seconds, fixation probability (in [0-1] range) when specifying 2 stimulus fixations in the Experiment Control Settings. These variables can be used to introduce a fixation task (change detection) to control for attention in experiments where a succession of stimuli are presented. The read-only system-defined variable [FIXCHANGED] should then be used as the stimulus correlated expression in a Yes/No Procedure (see above).
  [PAUSETRIALS], [PAUSETIME], [PAUSEDURATION], [PAUSEMSG]Variables to trigger a pause at regular intervals or after a number of trials: use PAUSETIME to specify the interval in seconds between the pauses, use PAUSETRIALS to indicate the number of trials between the pauses, specify the pause duration in seconds using PAUSEDURATION, some custom text can be displayed during the pause using PAUSEMSG (C string between " "). Note that PAUSETIME and PAUSETRIALS are mutually exclusive, and one of them with PAUSEDURATION are mandatory for the pause to occur.
  [TAPERIOD]Period of the trial alarm timer in seconds. The "Trial Alarm" feature is only available for some button boxes with an internal timer (see the "Supported External Devices" chapter). This variable can be also used to specify the interval for the Timer option for triggering the next trial in the Experiment Control Settings.

© 2006-2024 KyberVision Japan LLC. All rights reserved.